<?php


namespace app\admin\controller;
use app\admin\model\Foods as FoodsModel;
use app\admin\model\Sclass as SclassModel;
use think\Controller;

class Foods extends Controller
{
//查询美食
    public function selfoods(){

        $model = new FoodsModel();
        $data = $model->getFinfo();
        $this->assign('data', $data);
        return $this->fetch('selFoods');
    }

    //添加美食
    public function addfoods(){
        $model = new SclassModel();
        $sdata = $model->getupsFoods();
        $this->assign('sdata',$sdata);
        return $this->fetch();
    }

    public function doadd(){

        $data=$this->request->param();
        $name=$data['name'];
        $price=$data['price'];
        $orderno=$data['orderno'];
        $cid=$data['cid'];
        $describe=$data['describe'];
        $ins=[
            'name'=>$name,
            'price'=>$price,
            'orderno'=>$orderno,
            'cid'=>$cid,
            'describe'=>$describe
        ];

        // 获取表单上传文件
//        var_dump($data);
        $file = request()->file('pic');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $pic = $info->getSaveName();
                $ins['pic'] = $pic;
                $model = new FoodsModel();
                $res = $model->upload($ins);
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }

        }
        if($res){
            $this->success('添加成功','Foods/selFoods');
        }else{
            $this->error('添加失败','Foods/addFoods');
        }
    }
    //修改美食
    public function upFoods($no){
        $model = new FoodsModel();
        $data = $model->getupFoods($no);
        $this->assign('data', $data);
        $model = new SclassModel();
        $sdata = $model->getupsFoods();
        $this->assign('sdata',$sdata);
        return $this->fetch('upFoods');
    }

    public function up(){
        $data=$this->request->param();

//        $file = $this->upload('./Uploads/Foods/',true,120,120,false);
        $model=new FoodsModel();
        $res=$model->updateFood($data);
//        if($file){
//            $_POST['pic'] = $file;
//        }else{
            if ($res) {
               $this->success('修改成功','Foods/selFoods');
            } else {
                $this->error('修改失败','Foods/upFoods');
            }
        }
   //}

    //删除操作
    public function del($no){

        $model = new FoodsModel();

        if($model->delFood($no)){
           return alert_success('删除数据成功!','Foods/selFoods');
        }else{
            return alert_error('删除数据失败!');
        }

    }

}